import 'package:card_topia/common_widget/my_app_bar.dart';
import 'package:card_topia/common_widget/my_scaffold.dart';
import 'package:card_topia/design_specifications.dart';
import 'package:card_topia/main.dart';
import 'package:card_topia/my_router.dart';
import 'package:card_topia/service/login_service.dart';
import 'package:ficonsax/ficonsax.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../common_provider/page_provider.dart';
import '../../common_provider/user_provider.dart';
import '../../common_widget/bottom_button.dart';
import '../../utils/dialog_util.dart';
import '../../utils/event_util.dart';

///密码登录页面

class LoginPwdPage extends ConsumerStatefulWidget {
  const LoginPwdPage({super.key});

  @override
  ConsumerState<LoginPwdPage> createState() => _LoginPageState();
}

class _LoginPageState extends ConsumerState<LoginPwdPage> {
  final TextEditingController _controller = TextEditingController();
  final FocusNode _focusNode = FocusNode();
  final TextEditingController _pwdController = TextEditingController();
  final FocusNode _pwdFocusNode = FocusNode();
  bool _enable = false;
  bool _obscure = true;
  bool _agree = false;
  late final pagePod = ref.read(pageProvider);

  _loginByPwd()async{
    DialogUtil.showLoading();
    final result = await LoginService.loginByPwd(email: _controller.text, pwd: _pwdController.text);
    if(result.success){
      final success = await ref.read(userInfoProvider).setUserInfo(result.data);
      if(success){
        if(context.mounted){
          EventUtil.eventBus.fire(TabChangeEvent(pagePod.aimPage));
          MyRouter.push(page: MyHomePage());
        }
      }
      DialogUtil.dismiss();
    }else{
      EasyLoading.showToast("${result.msg}");
    }
  }
  @override
  void initState() {
    super.initState();
    _controller.addListener(() {
      listen();
    });
    _pwdController.addListener(() {
      listen();
    });
  }

  //监听输入框
  listen() {
    if (_controller.text.isNotEmpty&&_pwdController.text.isNotEmpty) {
      _enable = true;
    } else {
      _enable = false;
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: const CustomAppBar(
        showLeading: true,
      ),
      body: Column(
        children: [
          Container(
            margin: EdgeInsets.only(left: 16.w, top: 24.w),
            alignment: Alignment.centerLeft,
            child: Text(
              "密码登录",
              style: TextStyle(fontFamily: 'Harmony', fontSize: 24.sp),
            ),
          ),
          //邮箱输入
          SizedBox(
            height: 24.w,
          ),
          Container(
            decoration: BoxDecoration(
                color: Colors.white, borderRadius: BorderRadius.circular(8)),
            margin: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.w),
            padding: EdgeInsets.symmetric(
              horizontal: 12.w,
            ),
            height: 48.w,
            child: Row(
              children: [
                Icon(IconsaxOutline.sms,color: _controller.text.isNotEmpty?textColor:disableColor,),
                SizedBox(
                  width: 12.w,
                ),
                Expanded(
                  child: TextField(
                    focusNode: _focusNode,
                    controller: _controller,
                    autofocus: true,
                    cursorColor: themeColor,
                    keyboardType: TextInputType.emailAddress,
                    inputFormatters: [
                      FilteringTextInputFormatter.deny(RegExp(r'\s'))
                    ],
                    style: TextStyle(
                        fontFamily: harmony,
                        fontSize: 16.sp,
                        color: textColor),
                    decoration: InputDecoration(
                        border: InputBorder.none,
                        isCollapsed: true,
                        hintText: "请输入邮箱",
                        hintStyle: TextStyle(
                            fontFamily: harmony,
                            fontSize: 16.sp,
                            color: disableColor)),
                  ),
                ),
              ],
            ),
          ),
          Container(
            decoration: BoxDecoration(
                color: Colors.white, borderRadius: BorderRadius.circular(8)),
            margin: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.w),
            padding: EdgeInsets.symmetric(
              horizontal: 12.w,
            ),
            height: 48.w,
            child: Row(
              children: [
                Icon(IconsaxOutline.password_check,color: _pwdController.text.isNotEmpty?textColor:disableColor,),
                SizedBox(
                  width: 12.w,
                ),
                Expanded(
                  child: TextField(
                    focusNode: _pwdFocusNode,
                    controller: _pwdController,
                    autofocus: true,
                    cursorColor: themeColor,
                    obscureText: _obscure,
                    inputFormatters: [
                      FilteringTextInputFormatter.deny(RegExp(r'\s'))
                    ],
                    style: TextStyle(
                        fontFamily: harmony,
                        fontSize: 16.sp,
                        color: textColor),
                    decoration: InputDecoration(
                        border: InputBorder.none,
                        isCollapsed: true,
                        hintText: "请输入密码",
                        hintStyle: TextStyle(
                            fontFamily: harmony,
                            fontSize: 16.sp,
                            color: disableColor)),
                  ),
                ),
                InkWell(
                  onTap: ()=>setState(() {
                    _obscure = !_obscure;
                  }),
                  child: Icon(_obscure?IconsaxOutline.eye_slash:IconsaxOutline.eye,size: 20.w,
                  color: _obscure?disableColor:textColor,
                  ),
                )
              ],
            ),
          ),

          Container(
            alignment: Alignment.centerRight,
            padding: EdgeInsets.symmetric(horizontal: 16.w),
            child: Text("忘记密码",style: TextStyle(fontSize: 12.sp,color: disableColor),),
          ),
          SizedBox(height: 32.w,),
          BottomButton(
              text: "登录" ,
              enable: _enable,
              onTap: () {
                FocusScope.of(context).requestFocus(FocusNode());
                _loginByPwd();
                // viewModel.loginByPwd(context);
              }),
          SizedBox(height: 16.w,),
          InkWell(
            onTap: ()=>setState(() {
              _agree = !_agree;
            }),
            child: Container(
              margin: EdgeInsets.symmetric(horizontal: 16.w),
              child: Row(
                children: [
                  Icon(_agree?Icons.radio_button_checked_rounded:Icons.circle_outlined,size: 18.w,color: _agree?themeColor:disableColor,),
                  SizedBox(width: 4.w,),
                  Text("已阅读并同意",style: TextStyle(fontFamily: harmony,fontSize: 12.sp),),
                  Text("用户协议",style: TextStyle(fontFamily: harmony,fontSize: 12.sp,color: Colors.blue),),
                  Text("和",style: TextStyle(fontFamily: harmony,fontSize: 12.sp),),
                  Text("隐私政策",style: TextStyle(fontFamily: harmony,fontSize: 12.sp,color: Colors.blue),),
                ],
              ),
            ),
          ),


        ],
      ),
    ).withOnTap(context);
  }
}
